var LOC_MOVIESLIST = 0;
var LOC_MAINLINK = 1;
var CURRENT_LOCATION = 0;

var PAGE_SIZE=7;

var animating = false;
var missedCall = false;

function screenEnable(enabled){
	actionsEnabled = enabled;
}

//movies list listener
addListener(menuBar = function(actionKey, currentLocation){
	if(currentLocation == LOC_MOVIESLIST){
		switch(actionKey){
			case ACTION_KEYUP_LEFT:
			keyUp_LeftMoviesList();
			break;
			case ACTION_KEYDOWN_LEFT:
			keyDown_LeftMoviesList();
			break;
			case ACTION_KEYUP_RIGHT:
			keyUp_RightMoviesList();
			break;
			case ACTION_KEYDOWN_RIGHT:
			keyDown_RightMoviesList();
			break;
			case ACTION_KEYDOWN_UP:
			keyDown_UpMoviesList();
			break;
			case ACTION_KEYUP_UP:
			keyUp_UpMoviesList();
			break;
			case ACTION_KEYDOWN_DOWN:
			keyDown_DownMoviesList();
			break;
			case ACTION_KEYUP_DOWN:
			keyUp_DownMoviesList();
			break;
			case ACTION_KEYUP_ENTER:
			keyEnterMoviesList();
			break;
			case ACTION_KEYDOWN_DOUBLEKEY:
			keyDoubleMoviesList();
			break;
		}
	}
});


//main link listener
addListener(menuBar = function(actionKey, currentLocation){
	if(currentLocation == LOC_MAINLINK){
		switch(actionKey){
			case ACTION_KEYUP_RIGHT:
			keyUp_RightMainLink();
			break;
			case ACTION_KEYUP_DOWN:
			keyUp_DownMainLink();
			break;
			case ACTION_KEYUP_ENTER:
			keyUp_EnterMainLink();
			break;
			case ACTION_KEYDOWN_DOUBLEKEY:
			keyDoubleMainLink();
			break;
		}
	}
});


/****************************************/
//main link action methods
/****************************************/
function keyUp_RightMainLink(){
/*	CURRENT_LOCATION = LOC_SORTLINK;
	
	$('homeIcon').removeClassName('homeIconSelected');
	$('homeIcon').addClassName('homeIcon');
	
	$('sortIcon').removeClassName('sortIcon');
	$('sortIcon').addClassName('sortIconSelected');
	*/
}

function keyUp_DownMainLink(){
	/*
	CURRENT_LOCATION = LOC_MOVIESLIST;
	
	$('homeIcon').removeClassName('homeIconSelected');
	$('homeIcon').addClassName('homeIcon');
	
	selectMovieRow("movieRow"+movieListIndex);
	*/
}

function keyDoubleMainLink(){
	CURRENT_LOCATION = LOC_MOVIESLIST;
	
	//$('homeIcon').removeClassName('homeIconSelected');
	//$('homeIcon').addClassName('homeIcon');
	
	hideTopMenuOverlay();
	
	selectMovieRow("movieRow"+movieListIndex);
}

function keyUp_EnterMainLink(){
	location.href = 'index.php';
}

/****************************************/
//movie list action methods
/***************************************/
function keyDown_LeftMoviesList(){
	var oldIndex = movieListIndex;
	movieListIndex = movieListIndex-PAGE_SIZE;
	
	if(movieListIndex < 0){
		movieListIndex = 0;	
	}
	
	if(oldIndex != movieListIndex){
		htmlRowIndex = htmlRowIndex-PAGE_SIZE;
	}
	
	if(htmlRowIndex < 0){
		var fromPage = htmlPageIndex;
		setPageIndex();
		scrollMovieList(fromPage, htmlPageIndex);
		setRowIndex();
	}
	
	unselectMovieRow("movieRow"+oldIndex);
	selectMovieRow("movieRow"+movieListIndex);
	updateScrollbar(oldIndex, movieListIndex);
}

function keyUp_LeftMoviesList(){
	if(!animating){
		missedCall = false;
		var movieRow = $("movieRow"+movieListIndex);
		updateMovieInfo(movieRow.readAttribute('tmdb_id'),'movieRow'+movieListIndex, movieRow.readAttribute('certification'), movieRow.readAttribute('disc_value'), movieRow.readAttribute('audio_value'));
	}else{
		missedCall = true;
	}
}

function keyDown_RightMoviesList(){
	var oldIndex = movieListIndex;
	movieListIndex = movieListIndex+PAGE_SIZE;
	
	if(movieListIndex > (movieListLength-1)){
		movieListIndex = movieListLength-1;
	}
	
	if(oldIndex != movieListIndex){
		htmlRowIndex = htmlRowIndex+PAGE_SIZE;
	}
	
	var movieRow = $("movieRow"+movieListIndex);
	if(htmlRowIndex > 6){
		var fromPage = htmlPageIndex;
		setPageIndex();
		scrollMovieList(fromPage, htmlPageIndex);
		setRowIndex();
	}
	
	unselectMovieRow("movieRow"+oldIndex);
	selectMovieRow("movieRow"+movieListIndex);
	updateScrollbar(oldIndex, movieListIndex);
}

function keyUp_RightMoviesList(){
	if(!animating){
		missedCall = false;
		var movieRow = $("movieRow"+movieListIndex);
		updateMovieInfo(movieRow.readAttribute('tmdb_id'),'movieRow'+movieListIndex, movieRow.readAttribute('certification'), movieRow.readAttribute('disc_value'), movieRow.readAttribute('audio_value'));
	}else{
		missedCall = true;
	}
}

function keyDown_UpMoviesList(){
	var oldIndex = movieListIndex;
	movieListIndex = movieListIndex-getIndexIncrement();
	
	if(movieListIndex < 0){
		movieListIndex = 0;	
	}
	
	if(oldIndex != movieListIndex){
		htmlRowIndex = htmlRowIndex-getIndexIncrement();
	}
	
	if(htmlRowIndex < 0){
		var fromPage = htmlPageIndex;
		setPageIndex();
		scrollMovieList(fromPage, htmlPageIndex);
		setRowIndex();
	}
	
	unselectMovieRow("movieRow"+oldIndex);
	selectMovieRow("movieRow"+movieListIndex);
	updateScrollbar(oldIndex, movieListIndex);
}

function keyUp_UpMoviesList(){
	if(!animating){
		missedCall = false;
		var movieRow = $("movieRow"+movieListIndex);
		updateMovieInfo(movieRow.readAttribute('tmdb_id'),'movieRow'+movieListIndex, movieRow.readAttribute('certification'), movieRow.readAttribute('disc_value'), movieRow.readAttribute('audio_value'));
	}else{
		missedCall = true;
	}
}

function keyDown_DownMoviesList(){
	var oldIndex = movieListIndex;
	movieListIndex = movieListIndex+getIndexIncrement();
	
	if(movieListIndex > (movieListLength-1)){
		movieListIndex = movieListLength-1;
	}
	
	if(oldIndex != movieListIndex){
		htmlRowIndex = htmlRowIndex+getIndexIncrement();
	}
	
	var movieRow = $("movieRow"+movieListIndex);
	if(htmlRowIndex > 6){
		var fromPage = htmlPageIndex;
		setPageIndex();
		scrollMovieList(fromPage, htmlPageIndex);
		setRowIndex();
	}
	
	unselectMovieRow("movieRow"+oldIndex);
	selectMovieRow("movieRow"+movieListIndex);
	updateScrollbar(oldIndex, movieListIndex);
}

function keyUp_DownMoviesList(){
	if(!animating){
		missedCall = false;
		var movieRow = $("movieRow"+movieListIndex);
		updateMovieInfo(movieRow.readAttribute('tmdb_id'),'movieRow'+movieListIndex, movieRow.readAttribute('certification'), movieRow.readAttribute('disc_value'), movieRow.readAttribute('audio_value'));
	}else{
		missedCall = true;
	}
}

function keyDoubleMoviesList(){
	CURRENT_LOCATION = LOC_MAINLINK;

	unselectMovieRow("movieRow"+movieListIndex);
	
	showTopMenuOverlay();
	
	//$('homeIcon').removeClassName('homeIcon');
	//$('homeIcon').addClassName('homeIconSelected');
}

function keyEnterMoviesList(){
	var td = $("movieRow"+movieListIndex);
	
	location.href = td.readAttribute('link');
}

function setPageIndex(){
	htmlPageIndex = ~~(movieListIndex / PAGE_SIZE);
}

function setRowIndex(){
	htmlRowIndex = (movieListIndex % PAGE_SIZE);
}

function scrollMovieList(fromPage, toPage){
	animating = true;
	var an = new Animator(
						  {duration: 500,
						   onComplete: function(){
							  animating = false;
							  
							  if(missedCall){
									keyUp_DownMoviesList();							
							  }
						  }}).addSubject(
	new NumericalStyleSubject(
		"movieScroll",
		"margin-top",
		(((fromPage*7)*-1)*114),
		(((toPage*7)*-1)*114)));
	
	an.toggle();
}

function updateScrollbar(fromIndex, toIndex){
	var fromPercent = ((fromIndex)/movieListLength)*100;
	var toPercent = ((toIndex)/movieListLength)*100;
	var an = new Animator().addSubject(
	new NumericalStyleSubject(
		"scrollNub",
		"margin-top",
		(fromPercent*7.86),
		(toPercent*7.86)));
	an.toggle();
}

function selectMovieRow(movieRow){
	$(movieRow).removeClassName('movieUnselected');
	$(movieRow).addClassName('movieSelected');
}
	 
function unselectMovieRow(movieRow)
{
 	$(movieRow).removeClassName('movieSelected');
	$(movieRow).addClassName('movieUnselected');
}